# coding:utf-8

"""
CopyRight(c)  2017-09-15  Fraplus Corp

Author: Weilin Shen, Xiaodong.yang
Email:  xiaodong.yang@unidt.com
Functions:

we chat server

dependence package:

fralog

"""
import platform
import json
import requests
import pymongo
from redis import Redis
from fralog.FraLog import Fralog

log = Fralog().getFileLog()

url = 'http://wx.zhishixia.com/wechat/getuser'

if platform.system() == 'Windows':
    port = 27017
elif platform.system() == 'Linux':
    port = 27137
else:
    port = 27017


def get_user(openid):
    params = {}
    params['openid'] = openid
    r = requests.post(url=url, data=params)
    return json.loads(r.text)


if __name__ == '__main__':
    params = {}
    #
    # 遍历获得目前所有的openid
    client = pymongo.MongoClient("127.0.0.1", port)  # 连接数据库
    db = client.survey_database
    db.responses_collection.create_index([('code', pymongo.ASCENDING), ('response.wxid', pymongo.ASCENDING)])
    db.user_collection.remove()
    # resps = db.responses_collection.distinct("response.wxid")
    # for r in resps:
    #     user = get_user(r)
    #     if user['err'] != 0:
    #         continue
    #     print(user['data']['nickname'])
    #     db.user_collection.insert(user['data'])
